//
//  ClockFooterView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/13.
//

#import "ClockFooterView.h"
#import "FishPondPhoneView.h"

#import "VoiceView.h"

@interface ClockFooterView ()
@property (nonatomic ,strong) UIView * topview;

@property (nonatomic ,strong) FishPondPhoneView * onephone;
@property (nonatomic ,strong) FishPondPhoneView * twephone;
@property (nonatomic ,strong) FishPondPhoneView * threephone;
@property (nonatomic ,strong) FishPondPhoneView * fourphone;
@property (nonatomic ,strong) FishPondPhoneView * fivephone;
@property (nonatomic ,strong) FishPondPhoneView * sixphone;
@property (nonatomic ,strong) VoiceView * voice;

@end

@implementation ClockFooterView


+ (instancetype)NewView {
    ClockFooterView * view = [ClockFooterView new];
    view.backgroundColor = UIColor.whiteColor;
    view.clipsToBounds = YES;
    [view newUI];
    return view;
}
- (void)newUI{
    
    MJWeakSelf
    UIView * topview = [UIView new];
    self.topview = topview;
    [self addSubview:topview];
    topview.backgroundColor = KcolorHex(@"#F6F7F9");
    [DTKTool YJ:kwt(5) andView:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(235));
    }];
    
    UILabel * templab = [DTKTool labelWithText:@"请拍摄您的打卡照片" andFont:kFont(18) andTextColor:KcolorHex(@"#222222")];
    [topview addSubview:templab];
    [templab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(15));
        make.left.offset(kwt(12));
    }];
    
    
    FishPondPhoneView * onephone = [FishPondPhoneView NewView];
    self.onephone = onephone;
    onephone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:0];
    };
    onephone.addImgBlock = ^{
        [weakSelf addImage];
    };
    onephone.openImgBlock = ^{
        [weakSelf openImageWithIndex:0];
    };
    [onephone setContent:@"还可添加6张"];
    [topview addSubview:onephone];
    [onephone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.offset(kwt(48));
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    
    FishPondPhoneView * twephone = [FishPondPhoneView NewView];
    self.twephone = twephone;
    [twephone setContent:@"还可添加5张"];
    twephone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:1];
    };
    twephone.addImgBlock = ^{
        [weakSelf addImage];
    };
    twephone.openImgBlock = ^{
        [weakSelf openImageWithIndex:1];
    };
    [topview addSubview:twephone];
    [twephone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(onephone.mas_right).offset(kwt(5));
        make.top.offset(kwt(48));
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    FishPondPhoneView * threephone = [FishPondPhoneView NewView];
    self.threephone = threephone;
    [threephone setContent:@"还可添加4张"];
    threephone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:2];
    };
    threephone.addImgBlock = ^{
        [weakSelf addImage];
    };
    threephone.openImgBlock = ^{
        [weakSelf openImageWithIndex:2];
    };
    [topview addSubview:threephone];
    [threephone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(twephone.mas_right).offset(kwt(5));
        make.top.offset(kwt(48));
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    FishPondPhoneView * fourphone = [FishPondPhoneView NewView];
    self.fourphone = fourphone;
    fourphone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:3];
    };
    fourphone.addImgBlock = ^{
        [weakSelf addImage];
    };
    fourphone.openImgBlock = ^{
        [weakSelf openImageWithIndex:3];
    };
    [fourphone setContent:@"还可添加3张"];
    [topview addSubview:fourphone];
    [fourphone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(12));
        make.top.equalTo(onephone.mas_bottom).offset(kwt(10));
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    
    FishPondPhoneView * fivephone = [FishPondPhoneView NewView];
    self.fivephone = fivephone;
    [fivephone setContent:@"还可添加2张"];
    fivephone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:4];
    };
    fivephone.addImgBlock = ^{
        [weakSelf addImage];
    };
    fivephone.openImgBlock = ^{
        [weakSelf openImageWithIndex:4];
    };
    [topview addSubview:fivephone];
    [fivephone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(fourphone.mas_right).offset(kwt(5));
        make.top.equalTo(fourphone);
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    FishPondPhoneView * sixphone = [FishPondPhoneView NewView];
    self.sixphone = sixphone;
    [sixphone setContent:@"还可添加1张"];
    sixphone.deleteBlock = ^{
        [weakSelf deleteImgWithIndex:5];
    };
    sixphone.addImgBlock = ^{
        [weakSelf addImage];
    };
    sixphone.openImgBlock = ^{
        [weakSelf openImageWithIndex:5];
    };
    [topview addSubview:sixphone];
    [sixphone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(fivephone.mas_right).offset(kwt(5));
        make.top.equalTo(fivephone);
        make.width.offset(kwt(105));
        make.height.offset(kwt(82));
    }];
    
    VoiceView * voice = [VoiceView NewView];
    [DTKTool YJ:kwt(5) andView:voice];
    self.voice = voice;
    voice.SoundBlock = ^(NSString * _Nonnull Url, NSString * _Nonnull previewUrl) {
        weakSelf.model.TJvoiceUrl = Url;
        weakSelf.model.voiceUrl = previewUrl;
        if(weakSelf.reloadBlock){
            weakSelf.reloadBlock();
        }
    };
    voice.removeBlock = ^{
        weakSelf.model.TJvoiceUrl = @"";
        weakSelf.model.voiceUrl = @"";
        if(weakSelf.reloadBlock){
            weakSelf.reloadBlock();
        }
    };
    [self addSubview:voice];
    [voice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topview.mas_bottom).offset(kwt(10));
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(55));

    }];
    
}

- (void)deleteImgWithIndex:(int)i{
    if(self.deleteImageBlock){
        self.deleteImageBlock(self.model, i);
    }
}
- (void)addImage{
//    [self.btn playVoice];
//    return;
    if(self.openAddImageBlock){
        self.openAddImageBlock(self.model);
    }
}
- (void)openImageWithIndex:(int)i{
    if(self.openImageBlock){
        self.openImageBlock(self.model, i);
    }
}

- (void)setModel:(ClockModel *)model{
    _model = model;
    MJWeakSelf
    model.openDurationBlock = ^(NSString * _Nonnull voiceDuration) {
        weakSelf.voice.voiceDuration = voiceDuration;
    };
    self.voice.voiceUrl = model.voiceUrl;
    self.voice.voiceDuration = model.voiceDuration;
    [self.topview mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.offset(model.FooterPhoneViewH);
    }];
    int i = 0;
    for (FishPondPhoneView * phone in @[self.onephone,self.twephone,self.threephone,self.fourphone,self.fivephone,self.sixphone]) {
        if(model.cardImgArray.count > i){
            [phone setImgUrl:model.cardImgArray[i]];
            phone.hidden = NO;
        } else if(model.cardImgArray.count == i){
            [phone deleteImg];
            phone.hidden = NO;
        } else {
            phone.hidden = YES;
        }
        i++;
    }
}
@end
